--freeswitch.consoleLog("notice", "Debug from auth_user.lua, provided params:\n" .. params:serialize() .. "\n")
 
local req_domain = params:getHeader("domain")
local req_key    = params:getHeader("key")
local req_user   = params:getHeader("user")
 
--assert (req_domain and req_key and req_user,  "This example script only supports generating directory xml for a single user !\n")
if req_user == nil then
   XML_STRING =
	[[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
	<document type="freeswitch/xml">
	  <section name="directory">
		<domain name="default">		   
		</domain>
	  </section>
	</document>]]
   return
end
 
local dbh = freeswitch.Dbh("mariadb://Server=127.0.0.1;Port=3306;Database=easycallcenter365;Uid=root;Pwd=easycallcenter365;")
if dbh:connected() == false then
  freeswitch.consoleLog("notice", "auth_user.lua cannot connect to database" .. dsn .. "\n")
  return
end
 
local my_query = string.format("select ext_num,ext_pass from cc_ext_num where  ext_num ='%s' limit 1", req_user)
 
assert (dbh:query(my_query, function(u)
  XML_STRING =
[[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="freeswitch/xml">
  <section name="directory">
    <domain name="]] .. req_domain .. [[">
      <user id="]] .. req_user .. [[">
        <params>
          <param name="password" value="]] .. u.ext_pass .. [["/>
          <param name="dial-string" value="{sip_invite_domain=${dialed_domain},presence_id=${dialed_user}@${dialed_domain}}${sofia_contact(${dialed_user}@${dialed_domain})}"/>
        </params>
        <variables>
          <variable name="user_context" value="default"/>
          <variable name="toll_allow" value="domestic,international,local"/>
          <variable name="accountcode" value="]] .. req_user .. [["/>
          <variable name="effective_caller_id_name" value="Extension ]] .. req_user .. [["/>
          <variable name="effective_caller_id_number" value="]] .. req_user .. [["/>
          <variable name="outbound_caller_id_name" value=""/>
          <variable name="outbound_caller_id_number" value=""/>
          <variable name="callgroup" value="techsupport"/>
        </variables>
      </user>
    </domain>
  </section>
</document>]]
 
 --freeswitch.consoleLog("notice", "Debug from auth_user.lua, generated XML:\n" .. XML_STRING .. "\n")
end))
